#ifndef DOUBLE_LIST_H
#define DOUBLE_LIST_H

#include <stddef.h>

/**
 * Definition of the list node structure
 */
struct list_head {
        struct list_head *next, *prev;
};

/**
 * Initialize the list head
 */
#define LIST_HEAD_INIT(name) { &(name), &(name) }

/**
 * Declare and initialize the list head
 */
#define LIST_HEAD(name) \
        struct list_head name = LIST_HEAD_INIT(name)

/**
 * Initialize the list head pointer
 */
#define INIT_LIST_HEAD(ptr) do { \
        (ptr)->next = (ptr); (ptr)->prev = (ptr); \
} while (0)

/**
 * Insert a new node between two known nodes
 */
static inline void __list_add(struct list_head *new,
                              struct list_head *prev,
                              struct list_head *next)
{
        next->prev = new;
        new->next = next;
        new->prev = prev;
        prev->next = new;
}

/**
 * Insert a new node at the head of the list
 */
static inline void list_add(struct list_head *new,
                            struct list_head *head)
{
        __list_add(new, head, head->next);
}

/**
 * Insert a new node at the tail of the list
 */
static inline void list_add_tail(struct list_head *new,
                                 struct list_head *head)
{
        __list_add(new, head->prev, head);
}

/**
 * Delete a node
 */
static inline void __list_del(struct list_head *prev,
                              struct list_head *next)
{
        next->prev = prev;
        prev->next = next;
}

/**
 * Delete a node and initialize it
 */
static inline void list_del(struct list_head *entry)
{
        __list_del(entry->prev, entry->next);
        entry->next = (void *) 0;
        entry->prev = (void *) 0;
}

/**
 * Check if the list is empty
 */
static inline int list_empty(const struct list_head *head)
{
        return head->next == head;
}

/**
 * Get the pointer to the structure that contains the specified member
 */

#ifndef offsetof
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#endif

#define container_of(ptr, type, member) ({            \
        const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
        (type *)( (char *)__mptr - offsetof(type,member) );})

#define list_entry(ptr, type, member) \
        container_of(ptr, type, member)

/**
 * Traverse the list
 */
#define list_for_each(pos, head) \
        for (pos = (head)->next; pos != (head); \
                        pos = pos->next)

/**
 * Safely traverse the list, allowing the current
 * node to be deleted during the traversal
 */
#define list_for_each_safe(pos, n, head) \
        for (pos = (head)->next, n = pos->next; pos != (head); \
            pos = n, n = pos->next)
/**
 * Iterate over list of given type
 */
#define list_for_each_entry(pos, head, member)				\
	for (pos = list_entry((head)->next, typeof(*pos), member);	\
	     &pos->member != (head);					\
	     pos = list_entry(pos->member.next, typeof(*pos), member))

/**
 * Safely traverse each entry in the list, allowing the
 * current node to be deleted during the traversal
 */
#define list_for_each_entry_safe(pos, n, head, member)            \
        for (pos = list_entry((head)->next, typeof(*pos), member),    \
                n = list_entry(pos->member.next, typeof(*pos), member); \
             &pos->member != (head);                            \
             pos = n, n = list_entry(n->member.next, typeof(*n), member))

#endif
